Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
Creating BLEU loss method on tensorflow gives "No gradient provided"

I need to build a custom loss method based on BLEU. I'm passing my LabelEncoder in the constructor to reverse labels and predictions and calculate the bleu distance.

Here is my Loss class

class CIMCodeSuccessiveLoss(Loss):

    def __init__(self, labelEncoder: LabelEncoder):
        super().__init__()
        self.le = labelEncoder

    def bleu_score(self, true_label, pred_label):
        cim_true_label = self.le.inverse_transform(true_label.numpy())
        cim_pred_label = self.le.inverse_transform(pred_label.numpy())
        bleu_scores = [sentence_bleu(list(one_true_label),
                                     list(one_pred_label),
                                     weights=(0.5, 0.25, 0.125, 0.125)) for one_true_label, one_pred_label in
                       zip(cim_true_label, cim_pred_label)]
        return np.float32(bleu_scores)

    def call(self, y_true, y_pred):
        labeled_y_pred = tf.cast(tf.argmax(y_pred, axis=-1), tf.int32)
        bleu = tf.py_function(self.bleu_score, (tf.reshape(y_true, [-1]), labeled_y_pred), tf.float32)
        return tf.reduce_sum(tf.square(1 - bleu))

The bleu_score method is calculating the correct scores and returns a NumPy array. when I try to return the squared sum, I get this error

raise ValueError(f"No gradients provided for any variable: {variable}.

I'm also providing the model:

inputs = tf.keras.Input(shape=(1,), dtype=tf.string)
x = vectorize_layer(inputs)
x = Embedding(vocab_size, embedding_dim, name="embedding")(x)
x = LSTM(units=32, name="lstm")(x)
outputs = Dense(classes_number, name="classification")(x)

model = tf.keras.Model(inputs=inputs, outputs=outputs, name="first_cim_classifier")

model.summary()


# we add early stopping for our model.
early_stopping = EarlyStopping(monitor='loss', patience=2)

model.compile(
    loss=CIMCodeSuccessiveLoss(le),
    optimizer=tf.keras.optimizers.Adam(),
    metrics=["accuracy", "crossentropy"],
    run_eagerly=True)

trained_model = model.fit(np.array(x_train), np.array(y_train), batch_size=64, epochs=10,
                          validation_data=(np.array(x_val), np.array(y_val)),
                          callbacks=[early_stopping])

Any help is appreciated. Thanks in advance.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

To calculate the loss function, you use the method 'tf.argmax(y_pred, axis=-1)',argmax is not differentiable and the automatic differentiation to calculate the gradients is not possible, you have to remove this method, for example (depending on your data) you can change the output layer to softmax and labels to one_hot.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda